/**
 * Copyright 2004 - 2007 Blue Bamboo International Inc. 
 *           All rights reserved.
 *
 *
 * Blue Bamboo PROPRIETARY/CONFIDENTIAL.
 *
 */

package com.googlecode.bluetools.common.security;

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Provider;
import java.security.SecureRandom;

public class KeyPairUtil extends SecurityUtil {

	public static KeyPair generateKeyPair(String keyAlgorithm, int nKeySize, Provider provider)
			throws NoSuchAlgorithmException, NoSuchProviderException {
		// We could request BC here in order to gain support for generating
		// > 2048 bit RSA keys also on Java 1.4. But unless there's a way
		// to eg. read JKS keystores containing such keys on Java 1.4,
		// that would just help the user shoot herself in the foot...
		KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(keyAlgorithm, provider);

		// Create a SecureRandom
		SecureRandom rand = SecureRandom.getInstance("SHA1PRNG");

		// Initialise key pair generator with key strength and a randomness
		keyPairGen.initialize(nKeySize, rand);

		// Generate and return the key pair
		KeyPair keyPair = keyPairGen.generateKeyPair();
		return keyPair;
	}

	public static KeyPair generateKeyPair(String keyAlgorithm, int nKeySize, String providerName)
			throws NoSuchAlgorithmException, NoSuchProviderException {
		// We could request BC here in order to gain support for generating
		// > 2048 bit RSA keys also on Java 1.4. But unless there's a way
		// to eg. read JKS keystores containing such keys on Java 1.4,
		// that would just help the user shoot herself in the foot...
		KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(keyAlgorithm, providerName);

		// Create a SecureRandom
		SecureRandom rand = SecureRandom.getInstance("SHA1PRNG");

		// Initialise key pair generator with key strength and a randomness
		keyPairGen.initialize(nKeySize, rand);

		// Generate and return the key pair
		KeyPair keyPair = keyPairGen.generateKeyPair();
		return keyPair;
	}

	public static KeyPair generateKeyPair(String keyAlgorithm, int nKeySize) throws NoSuchAlgorithmException {
		// We could request BC here in order to gain support for generating
		// > 2048 bit RSA keys also on Java 1.4. But unless there's a way
		// to eg. read JKS keystores containing such keys on Java 1.4,
		// that would just help the user shoot herself in the foot...
		KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(keyAlgorithm);

		// Create a SecureRandom
		SecureRandom rand = SecureRandom.getInstance("SHA1PRNG");

		// Initialise key pair generator with key strength and a randomness
		keyPairGen.initialize(nKeySize, rand);

		// Generate and return the key pair
		KeyPair keyPair = keyPairGen.generateKeyPair();
		return keyPair;
	}
}
